﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Restaurant.Modules.BIZZ.LocalService;
using Sector7.Restaurant.Modules.BIZZ.Utility;
using Sector7.Framework.Enums;
using Sector7.Restaurant.Biz;
using Sector7.Framework.Core;
using Sector7.Framework.Utility;

namespace Sector7.Restaurant.Modules.BIZZ.Windows
{
    /// <summary>
    /// PaymentWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PaymentWindow : Window
    {
        private RS_Table _table = null;
        private RS_SalesBill _salesBill = null;

        public PaymentWindow(RS_Table table)
        {
            InitializeComponent();
            this.Owner = Senvironments.MainWindow;

            _table = table;
        }

        private void LoadData()
        {
            _salesBill = Aide.LocalClient.Invoke<RS_SalesBill>(
                service => service.GetSalesBillForTable(_table.TableId));
            orderDetailControl.SalesBill = _salesBill;

            txbIn.Focus();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();
        }

        private void txbIn_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                decimal amount = orderDetailControl.SalesBill.Amount;
                decimal payin = Convert.ToDecimal(txbIn.Text);

                txbOut.Text = (payin - amount).ToString();
                txbOut.Focus();
            }
        }

        private void txbOut_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                EntityDelta delta = ClientUtil.CreateDelta<EntityDelta>(typeof(RS_SalesBill));
                delta.Conditions.Add("BillId", _salesBill.BillId);
                delta.DeltaProperties.Add("Status", (int)BillStatusType.Done);
                delta.DeltaProperties.Add("TableId", _salesBill.TableId);
                delta.DeltaProperties.Add("Amount", _salesBill.Amount);

                int result = Aide.LocalClient.Invoke<int>(
                    service => service.SetSalesBill(delta));

                this.DialogResult = result == 1;
            }
        }
    }
}
